package com.epichust.bootuqc.utils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * @author ：longyang
 * @date ：Created in 2020/12/21 15:10
 * @description：公共工具类
 */
public class CommonUtils {
    private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);

    /**
     * Clob转String
     * @param clob
     * @return
     * @throws SQLException
     * @throws IOException
     */
    public static String ClobToString(Clob clob) throws SQLException, IOException {
        String reString = "";
        Reader is = clob.getCharacterStream();// 得到流
        BufferedReader br = new BufferedReader(is);
        String s = br.readLine();
        StringBuffer sb = new StringBuffer();
        while (s != null) {// 执行循环将字符串全部取出付值给 StringBuffer由StringBuffer转成STRING
            sb.append(s);
            s = br.readLine();
        }
        reString = sb.toString();
        return reString;
    }

    /**
     * String转Clob
     * @param string
     * @return
     */
    public static Clob stringToClob(final String string) {
        if(null == string || string.trim().length() == 0){
            return null;
        }
        Clob c = null;
        try {
            c = new javax.sql.rowset.serial.SerialClob(string.toCharArray());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return  c;
    }

    /**
     * 获取list指定区间的数据
     * @param start
     * @param end
     * @return
     */
    public static List getLimitList(List list,int start,int end){
        List newList = list.subList(start,end);
        return newList;
    }

    /**
     * 判断是不是json字符串
     * @param content
     * @return
     */
    public static boolean isJson(String content) {
        Gson gson = new GsonBuilder().serializeNulls().create();
        try {
            gson.fromJson(content, HashMap.class);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
